<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">后台管理系统</div>
      <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="username">
            <template #prepend>
              <el-button :icon="User"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              type="password"
              placeholder="password"
              v-model="param.password"
              @keyup.enter="submitForm()"
          >
            <template #prepend>
              <el-button :icon="Lock"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm()">登录</el-button>
        </div>
        <el-checkbox class="login-tips" v-model="checked" label="记住密码" size="large"/>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref, reactive} from 'vue';
import {useRouter} from 'vue-router';
import {ElMessage} from 'element-plus';
import type {FormInstance, FormRules} from 'element-plus';
import {Lock, User} from '@element-plus/icons-vue';
import user from "../api/user"
import {md5} from "js-md5";

interface LoginInfo {
  username: string;
  password: string;
}

const lgStr = localStorage.getItem('login-param');
const defParam = lgStr ? JSON.parse(lgStr) : null;
const checked = ref(lgStr ? true : false);

const router = useRouter();
const param = reactive<LoginInfo>({
  username: defParam ? defParam.username : '',
  password: defParam ? defParam.password : '',
});

const rules: FormRules = {
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur',
    },
  ],
  password: [{required: true, message: '请输入密码', trigger: 'blur'}],
};
const login = ref<FormInstance>();
// const submitForm = (formEl: FormInstance | undefined) => {
//   if (!formEl) return;
//   formEl.validate((valid: boolean) => {
//     if (valid) {
//       ElMessage.success('登录成功');
//       localStorage.setItem('ms_username', param.username);
//       const keys = permiss.defaultList[param.username == 'admin' ? 'admin' : 'user'];
//       permiss.handleSet(keys);
//       localStorage.setItem('ms_keys', JSON.stringify(keys));
//       router.push('/');
//       if (checked.value) {
//         localStorage.setItem('login-param', JSON.stringify(param));
//       } else {
//         localStorage.removeItem('login-param');
//       }
//     } else {
//       router.push('/');
//       ElMessage.error('登录失败');
//       return false;
//     }
//   });
// };

const submitForm = () => {
  console.log("?")
  user.login(
      {
        username: param.username,
        password: md5(param.password),
      },
      (res) => {
        if (res.code == 200) {
          let token = res.data.token;
          console.log(res)
          localStorage.setItem("ms_username", param.username)
          localStorage.setItem("token", token);
          localStorage.setItem("refreshToken", res.data.refreshToken);
          localStorage.setItem("expiredTime", res.data.expiredTime);
          if (checked.value) {
            localStorage.setItem('login-param', JSON.stringify(param));
          } else {
            localStorage.removeItem('login-param');
          }
          router.push({name: "pageTable"});
        } else {
          ElMessage.error('登录失败');
        }
      }
  );
}

// const tags = useTagsStore();
// tags.clearTags();
</script>

<style scoped>
.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background-image: url(../assets/img/login-bg.jpg);
  background-size: 100%;
}

.ms-title {
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #333;
  font-weight: bold;
  padding-top: 10px;
}

.ms-login {
  width: 350px;
  border-radius: 5px;
  background: #fff;
}

.ms-content {
  padding: 10px 30px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}

.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #333;
}
</style>
